<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cms extends BaseController {

	public function __construct() {
        parent::__construct();
        // 加载库
        $this->load->model('cmsModel');
        $this->load->model('schoolModel');
    }

    // 首页
    public function index($token){
    	// 官网设置信息
    	$this->cmsInfo = $cmsInfo = $this->cmsModel->get_cms_setting_by_token($token);
        // 获取学校信息
        $this->schoolInfo = $schoolInfo = $this->schoolModel->get_school_info($cmsInfo['school_id']);

    	// 获取轮播图
    	$params['schoolId'] = $cmsInfo['school_id'];
    	$params['campusId'] = $cmsInfo['campus_id'];
    	$params['isDelete'] = 0;
    	$this->sliderList = $this->cmsModel->get_slider_list($params);

        // 获取一级分类列表
        $cateParams['schoolId'] = $schoolInfo['id'];
        $cateParams['campusId'] = $cmsInfo['campus_id'];
        $cateParams['parentId'] = 0;
        $cateParams['isDelete'] = 0;
        $this->categoryList = $this->cmsModel->get_category_list($cateParams);

        // 获取友情链接
        $linkParams['schoolId'] = $schoolInfo['id'];
        $linkParams['campusId'] = $cmsInfo['campus_id'];
        $linkParams['isDelete'] = 0;
        $this->linkList = $this->cmsModel->get_link_list($linkParams);

        // 获取推荐的普通栏目
        $topCate['schoolId'] = $schoolInfo['id'];
        $topCate['campusId'] = $cmsInfo['campus_id'];
        $topCate['cateType'] = CMS_CATEGORY_NORMAL;
        $topCate['cateRecommend'] = 1;
        $topCate['isDelete'] = 0;
        $this->topCateList = $this->cmsModel->get_category_list($topCate);
        foreach($this->topCateList as &$topCate) {
            // 获取栏目下分类
            $childParams['parentId'] = $topCate['id'];
            $childParams['isDelete'] = 0;
            $childCate = $this->cmsModel->get_category_list($childParams);

            // 获取所有分类
            $childCate[] = array("id" => $topCate['id']);
            $cateIds = array_value_implode($childCate, "id");
            // 获取文章列表
            $articleParams['categoryId'] = $cateIds;
            $articleParams['isDelete'] = 0;
            $topCate['article'] = $this->cmsModel->get_article_list($articleParams);
        }

        // 获取推荐的文章栏目
        $topArticleCate['schoolId'] = $schoolInfo['id'];
        $topArticleCate['campusId'] = $cmsInfo['campus_id'];
        $topArticleCate['cateType'] = CMS_CATEGORY_ARTICLE;
        $topArticleCate['cateRecommend'] = 1;
        $topArticleCate['isDelete'] = 0;
        $this->articleCate = $this->cmsModel->get_category_list($topArticleCate);


        $this->display("cms". DIRECTORY_SEPARATOR. "index");
    }

    // 联系我们
    public function contact($token){
        // 官网设置信息
        $this->cmsInfo = $cmsInfo = $this->cmsModel->get_cms_setting_by_token($token);
        // 获取学校信息
        $this->schoolInfo = $schoolInfo = $this->schoolModel->get_school_info($cmsInfo['school_id']);
        // 获取一级分类列表
        $cateParams['schoolId'] = $schoolInfo['id'];
        $cateParams['campusId'] = $cmsInfo['campus_id'];
        $cateParams['parentId'] = 0;
        $cateParams['isDelete'] = 0;
        $this->categoryList = $this->cmsModel->get_category_list($cateParams);

        $this->display("cms". DIRECTORY_SEPARATOR. "contact");
    }

    // 分类
    public function category($token, $categoryId = 0){
        // 官网设置信息
        $this->cmsInfo = $cmsInfo = $this->cmsModel->get_cms_setting_by_token($token);
        // 获取学校信息
        $this->schoolInfo = $schoolInfo = $this->schoolModel->get_school_info($cmsInfo['school_id']);
        // 获取一级分类列表
        $cateParams['schoolId'] = $schoolInfo['id'];
        $cateParams['campusId'] = $cmsInfo['campus_id'];
        $cateParams['parentId'] = 0;
        $cateParams['isDelete'] = 0;
        $this->categoryList = $this->cmsModel->get_category_list($cateParams);

        // 如果是一级分类，则检查是否有子分类
        $cateParams['parentId'] = $categoryId;
        $this->childCategoryList = $childCategoryList = $this->cmsModel->get_category_list($cateParams);

        // 如果有子分类，则默认显示第一个子分类
        if(count($childCategoryList)) {
            $this->categoryInfo = $categoryInfo = current($childCategoryList);
        } else {
            $this->categoryInfo = $categoryInfo = $this->cmsModel->get_category_info($categoryId);

            // 否则，则检查是否有同级菜单
            if($categoryInfo['parent_id']) {
                $cateParams['parentId'] = $categoryInfo['parent_id'];
                $this->childCategoryList = $this->cmsModel->get_category_list($cateParams);
            }
        }

        // 获取上级菜单信息
        $this->parentCategory = null;
        if($categoryInfo['parent_id']) {
            $this->parentCategory = $this->cmsModel->get_category_info($categoryInfo['parent_id']);
        }
        
        // 获取下级文章列表
        $articleParams['categoryId'] = $categoryInfo['id'];
        $articleParams['isDelete'] = 0;
        $this->articleList = $this->cmsModel->get_article_list($articleParams);

        $this->display("cms". DIRECTORY_SEPARATOR. "category");
    }

    // 文章详情
    public function article($token, $articleId){
        // 官网设置信息
        $this->cmsInfo = $cmsInfo = $this->cmsModel->get_cms_setting_by_token($token);
        // 获取学校信息
        $this->schoolInfo = $schoolInfo = $this->schoolModel->get_school_info($cmsInfo['school_id']);
        // 获取文章信息
        $this->articleInfo = $articleInfo = $this->cmsModel->get_article_info($articleId);

        // 获取一级分类列表
        $cateParams['schoolId'] = $schoolInfo['id'];
        $cateParams['campusId'] = $cmsInfo['campus_id'];
        $cateParams['parentId'] = 0;
        $cateParams['isDelete'] = 0;
        $this->categoryList = $this->cmsModel->get_category_list($cateParams);

        // 获取分类信息
        $this->categoryInfo = $categoryInfo = $this->cmsModel->get_category_info($articleInfo['category_id']);

        // 获取同级菜单
        $cateParams['parentId'] = $categoryInfo['parent_id'];
        $this->childCategoryList = $this->cmsModel->get_category_list($cateParams);

        // 获取上级菜单信息
        $this->parentCategory = null;
        if($categoryInfo['parent_id']) {
            $this->parentCategory = $this->cmsModel->get_category_info($categoryInfo['parent_id']);
        }

        // 获取上一条数据
        $articleParams['schoolId'] = $articleInfo['school_id'];
        $articleParams['campusId'] = $articleInfo['campus_id'];
        $articleParams['categoryId'] = $articleInfo['category_id'];
        $articleParams['getPrev'] = $articleInfo['id'];
        $articleParams['isDelete'] = 0;
        $this->prevArticle = $this->cmsModel->get_article_next($articleParams);

        // 获取下一条数据
        unset($articleParams['getPrev']);
        $articleParams['getNext'] = $articleInfo['id'];
        $this->nextArticle = $this->cmsModel->get_article_next($articleParams);

        $this->display("cms". DIRECTORY_SEPARATOR. "article");
    }
}
